package ip.ejb.session.facade;
/***********************************************************************
 * Module:  MovieFacadeBean.java
 * Author:  mojsilo
 * Purpose: Defines the Class MovieFacadeBean
 ***********************************************************************/

import ip.dto.CategoryDTO;
import ip.dto.CountryDTO;
import ip.dto.LanguageDTO;
import ip.dto.MovieDTO;
import ip.ejb.entity.CategoryLocal;
import ip.ejb.entity.CategoryLocalHome;
import ip.ejb.entity.CategoryPK;
import ip.ejb.entity.CountryLocal;
import ip.ejb.entity.CountryLocalHome;
import ip.ejb.entity.CountryPK;
import ip.ejb.entity.LanguageLocal;
import ip.ejb.entity.LanguageLocalHome;
import ip.ejb.entity.LanguagePK;
import ip.ejb.entity.MovieLocal;
import ip.ejb.entity.MovieLocalHome;
import ip.ejb.entity.MoviePK;
import ip.exceptions.ApplicationException;
import ip.exceptions.CanNotCreateNewMovieException;
import ip.exceptions.NoSuchMovieException;
import ip.exceptions.UnexpectedException;
import ip.util.EJBHomeFactory;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;
/**
 * @ejb.bean
 *   name="MovieFacade"
 *   type="Stateless"
 *   jndi-name="ip/ejb/facade/MovieFacade"
 *   local-jndi-name="ip/ejb/facade/MovieFacadeLocal"
 * @ejb.transaction type="Supports"
 */
public class MovieFacadeBean implements SessionBean
{

	private static final long serialVersionUID = 1L;
	private SessionContext ctx;
	private MovieLocalHome movieLocalHome = null;
	private CategoryLocalHome categoryLocalHome = null;
	private LanguageLocalHome languageLocalHome = null;
	private CountryLocalHome countryLocalHome = null;
	private MovieLocal movieLocal = null;	
	private MoviePK moviePK = null;
	private Logger logger = Logger.getLogger(this.getClass());

	/**
	 * @throws ApplicationException 
	 * @ejb.interface-method view-type="local"
	 */
	public void createMovie(MovieDTO movieDTO) throws ApplicationException{
		try {
			movieLocalHome = (MovieLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.MovieLocalHome.class);
			categoryLocalHome = (CategoryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CategoryLocalHome.class);
			countryLocalHome = (CountryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CountryLocalHome.class);
			languageLocalHome = (LanguageLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.LanguageLocalHome.class);					

			CategoryLocal categoryLocal = categoryLocalHome.findByPrimaryKey(new CategoryPK(movieDTO.getCategory().getCategoryId()));
			CountryLocal countryLocal = countryLocalHome.findByPrimaryKey(new CountryPK(movieDTO.getCountry().getCountryId()));
			LanguageLocal languageLocal =  languageLocalHome.findByPrimaryKey(new LanguagePK(movieDTO.getLanguage().getLanguageId()));

			movieLocalHome.create(
					categoryLocal, 
					countryLocal, 
					languageLocal, 
					movieDTO.getOriginalTitle(), 
					movieDTO.getTranslatedTitle(), 
					movieDTO.getDirector(), 
					movieDTO.getProducer(), 
					movieDTO.getYear(), 
					movieDTO.getDescription(), 
					movieDTO.getLength(), 
					movieDTO.getQuantity(), 
					movieDTO.getPrice()); 

		} catch (NamingException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		} catch (CreateException e) {
			logger.error(e);
			e.printStackTrace();
			throw new CanNotCreateNewMovieException(movieDTO);
		} catch (FinderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @throws ApplicationException 
	 * @ejb.interface-method view-type="local"
	 */
	public void updateMovie(MovieDTO movieDTO) throws ApplicationException{
		try {
			movieLocalHome = (MovieLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.MovieLocalHome.class);
			categoryLocalHome = (CategoryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CategoryLocalHome.class);
			countryLocalHome = (CountryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CountryLocalHome.class);
			languageLocalHome = (LanguageLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.LanguageLocalHome.class);					

			movieLocal = movieLocalHome.findByPrimaryKey(new MoviePK(movieDTO.getMovieId()));
			CategoryLocal categoryLocal = categoryLocalHome.findByPrimaryKey(new CategoryPK(movieDTO.getCategory().getCategoryId()));
			CountryLocal countryLocal = countryLocalHome.findByPrimaryKey(new CountryPK(movieDTO.getCountry().getCountryId()));
			LanguageLocal languageLocal =  languageLocalHome.findByPrimaryKey(new LanguagePK(movieDTO.getLanguage().getLanguageId()));



			movieLocal.setCategory(categoryLocal);
			movieLocal.setCountry(countryLocal);
			movieLocal.setLanguage(languageLocal);

			movieLocal.setDescription(movieDTO.getDescription());
			movieLocal.setDirector(movieDTO.getDirector());
			movieLocal.setLength(movieDTO.getLength());
			movieLocal.setOriginalTitle(movieDTO.getOriginalTitle());
			movieLocal.setPrice(movieDTO.getPrice());
			movieLocal.setProducer(movieDTO.getProducer());
			movieLocal.setQuantity(movieDTO.getQuantity());
			movieLocal.setTranslatedTitle(movieDTO.getTranslatedTitle());
			movieLocal.setYear(movieDTO.getYear());

		} catch (NamingException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		} catch (FinderException e) {
			logger.error(e);
			e.printStackTrace();
			throw new NoSuchMovieException(movieDTO);
		}
	}

	/**
	 * @throws ApplicationException 
	 * @ejb.interface-method view-type="local"
	 */
	public MovieDTO getMovie(MovieDTO movieDTO) throws ApplicationException{
		try {
//			MovieLocalHome = (MovieLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/MovieLocal");
			movieLocalHome = (MovieLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.MovieLocalHome.class);
			movieLocal = movieLocalHome.findByPrimaryKey(new MoviePK(movieDTO.getMovieId()));
			movieDTO = createMovieDTO(movieLocal);

		} catch (NamingException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		} catch (FinderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return movieDTO;

	}

	/**
	 * 
	 * @throws ApplicationException 
	 * @ejb.interface-method view-type="local"
	 */
	public Collection getAllMovies() throws ApplicationException{
		Collection moviesDTO = null;
		try {
//			movieLocalHome = (MovieLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/MovieLocal");
			movieLocalHome = (MovieLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.MovieLocalHome.class);
			Collection movies = movieLocalHome.findAll();

			if(movies.size()>0){
				moviesDTO = new ArrayList();
				Iterator iter = movies.iterator();

				while (iter.hasNext()) {
					movieLocal = (MovieLocal) iter.next();
					moviesDTO.add(createMovieDTO(movieLocal));
				}
			}

		} catch (NamingException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		} catch (FinderException e) {
			e.printStackTrace();			
		}
		return moviesDTO;
	}

	/**
	 * 
	 * @ejb.interface-method view-type="local"
	 */
	public MovieDTO createMovieDTO(MovieLocal movieLocal) {			
		MovieDTO movieDTO = null;
		if(movieLocal != null){
			movieDTO = new MovieDTO();
			movieDTO.setMovieId(movieLocal.getMovieId());
			movieDTO.setDescription(movieLocal.getDescription());
			movieDTO.setDirector(movieLocal.getDirector());
			movieDTO.setLength(movieLocal.getLength());
			movieDTO.setOriginalTitle(movieLocal.getOriginalTitle());
			movieDTO.setPrice(movieLocal.getPrice());
			movieDTO.setProducer(movieLocal.getProducer());
			movieDTO.setQuantity(movieLocal.getQuantity());
			movieDTO.setTranslatedTitle(movieLocal.getTranslatedTitle());
			movieDTO.setYear(movieLocal.getYear());
			movieDTO.setCountry(createCountryDTO(movieLocal.getCountry()));
			movieDTO.setCategory(createCategoryDTO(movieLocal.getCategory()));
			movieDTO.setLanguage(createLanguageDTO(movieLocal.getLanguage()));
		}
		return movieDTO;
	}

	private CategoryDTO createCategoryDTO(CategoryLocal categoryLocal) {			
		CategoryDTO categoryDTO = null;
		if(categoryLocal != null){
			categoryDTO = new CategoryDTO();
			categoryDTO.setCategoryId(categoryLocal.getCategoryId());
			categoryDTO.setName(categoryLocal.getName());
		}
		return categoryDTO;
	}
	
	private CountryDTO createCountryDTO(CountryLocal countryLocal) {			
		CountryDTO countryDTO = null;
		if(countryLocal != null){
			countryDTO = new CountryDTO();
			countryDTO.setCountryId(countryLocal.getCountryId());
			countryDTO.setName(countryLocal.getName());
		}
		return countryDTO;
	}

	private LanguageDTO createLanguageDTO(LanguageLocal languageLocal) {			
		LanguageDTO languageDTO = null;
		if(languageLocal != null){
			languageDTO = new LanguageDTO();
			languageDTO.setLanguageId(languageLocal.getLanguageId());
			languageDTO.setName(languageLocal.getName());
		}
		return languageDTO;
	}
	/**
	 * @ejb.create-method
	 */
	public void ejbCreate(){}
	public void ejbActivate(){}
	public void ejbPassivate(){}
	public void ejbRemove(){}
	public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException {
		this.ctx = ctx;
	}

}